// login.js
const app = getApp()
import {
  formatImage
} from '../../utils/formatImage';
import {
  requestApi
} from '../../api'
import {
  extend
} from '../../sdk/extend'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    mobile: '',
    checked: false,
    seller_image: {},
    success: false,
    id: '',
    session_key: '',
    nickName: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const self = this;
    this.data.id = options.id || ''
    this.getInfo();
    wx.login({
      success(res) {
        app.request({
          url: requestApi.getKey,
          method: 'POST',
          login: false,
          data: {
            js_code: res.code
          },
        }, (data) => {
          if (data.original.ret_code == 200) {
            self.setData({
              session_key: data.original.data.session_key,
              openid: data.original.data.openid
            })
          }
        }, (error) => {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: error,
          })
        })
      }
    })
  },

  sellerShop() {
    wx.navigateTo({
      url: '/pages/store/store?seller_uid=' + this.data.id,
    })
  },

  commit() {

    app.request({
      url: requestApi.registManager,
      method: 'POST',
      data: {
        username: this.data.mobile,
        manager_id: this.data.id,
        seller_name: this.data.seller_name,
        avatar_image: this.data.seller_image.full_path
      },
    }, (res) => {
      this.setData({
        showModal: 0,
        success: true
      })

    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    })
  },

  copyCode() {
    wx.setClipboardData({
      data: 'seller.ofashion.com.cn',
    })
  },

  checkedProtocol(e) {
    this.setData({
      checked: !this.data.checked
    })
  },

  getInfo() {
    app.request({
      url: requestApi.getManagerInfo,
      method: 'GET',
      data: {
        manager_id: this.data.id
      },
    }, (res) => {
      let data = res.original.manager_info;
      data.avatar_image_uri = formatImage.cdnImage(data.avatar_image_uri, formatImage.THUMBNAIL_M);
      this.setData({
        manager_info: data
      })

    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    })
  },

  getAvatar(e) {
    let file = e.detail.avatarUrl;
    wx.getImageInfo({
      src: file,
      success: (e) => {
        // console.log(e)
        this.uploadFile(e)
      }
    })
    // console.log(e)
    // this.uploadFile(file)
  },

  uploadFile(file) {
    const tempFilePaths = file.path;
    const tempFileSize = 100000000;
    // const tempArr = tempFilePaths.split('.')
    const tempFileType = file.type;
    const date = new Date()
    const timestamp = Math.floor(new Date() / 1000);
    const time = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`

    let authPath = 'mars' + `/${time}`

    const params = {
      auth_path: authPath,
      file_size: tempFileSize,
      timestamp: timestamp,
      file_type: tempFileType,
    }

    app.request({
      url: requestApi.webOssAuthUrl,
      methods: 'GET',
      data: params
    }, (res) => {
      let ossKey = res.original.oss_key

      wx.uploadFile({
        url: ossKey.host,
        filePath: tempFilePaths,
        name: 'file',
        formData: {
          name: tempFilePaths,
          key: ossKey.auth_path,
          OSSAccessKeyId: ossKey.accessid,
          policy: ossKey.policy,
          signature: ossKey.signature,
          success_action_status: '200',
        },
        success: (res) => {
          console.log(res)
          this.setData({
            seller_image: {
              full_path: ossKey.host + '/' + ossKey.auth_path,
              path: ossKey.auth_path,
              width: file.width,
              height: file.height
            }
          })
        },
        fail(error) {
          wx.showModal({
            title: '提示',
            content: error,
            showCancel: false
          })
        }
      })

    }, (error) => {
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
    })
  },

  onShow() {

  },

  agreement(e){
    let url = e.currentTarget.dataset.url;
    url = url==1? 'https://static.ofashion.com.cn/Contracts_and_Policies/sellers/OFashion%E8%BF%B7%E6%A9%99%E9%9A%90%E7%A7%81%E6%94%BF%E7%AD%96V1.0.html' : 'https://static.ofashion.com.cn/Contracts_and_Policies/sellers/OFashion%E8%BF%B7%E6%A9%99%E7%94%A8%E6%88%B7%E6%9C%8D%E5%8A%A1%E5%8D%8F%E8%AE%AEV1.0.html'
    console.log(url)
    wx.navigateTo({
      url: '/pages/webview1/webview1?url=' + url
    })
  },


  // 跳转协议页面
  readPurchaseNotes: function (e) {
    let protocolName = e.currentTarget.dataset.protocol
    let url = `/pages/goods/purchaseNotes/purchaseNotes?protocol_name=${protocolName}`

    wx.navigateTo({
      url: url,
    })
  },

  // 微信手机号快捷登录
  getPhoneNumber(e) {
    const {
      session_key,
      openid
    } = this.data;

    app.request({
      url: requestApi.getdecryption,
      method: 'POST',
      login: false,
      data: {
        encryptData: e.detail.encryptedData,
        iv: e.detail.iv,
        session_key: session_key
        // encryptData: encodeURIComponent(e.detail.encryptedData),
        // iv: encodeURIComponent(e.detail.iv),
        // session_key: encodeURIComponent(data.original.data.session_key)
      },
    }, (d) => {
      if (d.original.ret_code == 200) {
        this.setData({
          mobile: d.original.data.wx_return.phoneNumber,
        })
        if (this.data.checked) {
          this.setData({
            showModal: 1
          })
        }
      }
    }, (error) => {
      // wx.showModal({
      //     title: '提示',
      //     showCancel: false,
      //     content: error,
      // })
    })
  },

  showModal() {
    if (!this.data.checked) {
      wx.showToast({
        title: '请仔细阅读并同意相关协议',
        icon: 'none',
      })
      return;
    }
    this.setData({
      showModal: 1
    })
  },

  hideModal() {
    this.setData({
      showModal: 0
    })
  },

  stopScroll() { },
})